%% Draw Policy Frontier
% by Jaromir Benes
%
% Calculate the asymptotic std deviations of inflation and output under
% discretionary and commitment policies for a range of different weights on
% output in the loss function. Use the calculated points to draw policy
% frontiers epicting trade-offs faced by the central bank, and compare them
% for the two types of policies.

%% Clear Workspace
%
% Clear workspace, close all graphics figures, clear command window, and
% check the IRIS version.

clear;
close all;
home;
irisrequired 20140319;
%#ok<*NOPTS>

%% Load Discretion and Commitment Model Objects
%
% Load model objects with discretionary and commitment policies created
% previously in `read_model`.

load read_model.mat m2 m3;

%% Calculate ACF for a Number of Different Weights on Output
%
% To plot a policy frontier, calculate the model-implied std deviations of
% inflation and the output gap for a range of different weights on the
% output gap (keeping the weight on inflation the same). Create a vector of
% a total of N <?nPoints?> values between 0 and 1 <?linspace?>. Take the
% existing optimal polich models, `m2` and `m3`, create new model objects
% `M2` and `M3` by expanding the number of alternative parameterizations to
% N <?alter?>, assign a range of different values (prepared in the vector
% `lmb1`) to the `lmb1` parameter <?lmb1?> , and reset `lmb2` to zero
% <?lmb2?>. Calibrate the std deviations of demand and cost-push shocks to
% 1 and 1.5, respectively <?std?>.
%
% As the next step, resolve the model objects with the new parameters
% <?solve?>, and call the function `acf` to compute the autocovariance
% function <?acf?>. By default, the function `acf` returns the
% contemporaneous autocovariance matrices (and autocorrelation matrices as
% a second output argument, if requested). To request higher order
% autocovariances, use the option `'order='` -- here we do not need them.
% Because the model objects have N = 41 parameterizations each, the
% size of the covariance matrices `C2` and `C3` in 4th dimension is 41
% <?size?>.
%
% The diagonal entries in the covariance matrices are the variances of the
% respective variables; the order of the variables in the rows and columns
% of the covariance matrices can be obtained by a call to the function
% `rownames` <?rownames?> or `colnames` <?colnames?>, respectively. The
% function `select` called with the names of the requested variables
% returns the appropriate covariance submatrix <?select?> (a
% 2-by-2-by-1-by-41 submatrix in this case).
%
% Retrieve the diagonal elements and calculate the squarte roots to get the
% std deviations of inflation and the output gap <?stdpi?>. Finally,
% transform the 1-by-1-by-1-by-41 vectors into columns vectors <?vec?>.

N = 41; %?nPoints?
lmb1 = linspace(0,1,N) %?linspace?
std_e = 1; %?stdDevOutputShock?
std_u = 1.5; %?stdDevInflShock?

% ...
%
% Optimal discretion policy model.

M2 = alter(m2,N); %?alter?
M2.lmb1 = lmb1; %?lmb1?
M2.lmb2 = 0; %?lmb2?
M2.std_e = std_e; %?std?
M2.std_u = std_u;
M2 = solve(M2) %?solve?

C2 = acf(M2); %?acf?
size(C2) %?size?
rownames(C2) %?rownames?
colnames(C2) %?colnames?

C2 = select(C2,{'pi','y'}); %?select?
size(C2)

stdpi2 = sqrt(C2(1,1,1,:)); %?stdpi?
stdy2 = sqrt(C2(2,2,1,:));
stdpi2 = stdpi2(:); %?vec?
stdy2 = stdy2(:);

% ...
%
% Optimal commitment model.

M3 = alter(m3,N); %?alter?
M3.lmb1 = lmb1; %?lmb1?
M3.lmb2 = 0; %?lmb2?
M3.std_e = std_e; %?std?
M3.std_u = std_u;
M3 = solve(M3) %?solve?

C3 = acf(M3); %?acf?
size(C3) %?size?
C3 = select(C3,{'pi','y'}); %?select?
size(C3)

stdpi3 = sqrt(C3(1,1,1,:)); %?stdpi?
stdy3 = sqrt(C3(2,2,1,:));
stdpi3 = stdpi3(:); %?vec?
stdy3 = stdy3(:);

%% Plot and Annotate Points on Frontier
%
% Plot the policy frontiers for the two optimal policy models as points in
% a plane with the std deviations of inflation on the horizontal axis and
% the std deviations of the output gap on the vertical axis <?plot?>.
% Annotate every 10 points on the frontier with the respective value of
% `lmb1` (the weight on output in the loss function) <?annotate?>.
%
% The policy frontier for the optimal discretion policy model lies somewhat
% above the frontier for the optimal commitment policy model, a result of
% the fact that the central bank does not manipulate the expectations to
% achieve global optimum.

figure();
hold all;
grid on;
h2 = plot(stdpi2(:),stdy2(:),'marker','.'); %?plot?
h3 = plot(stdpi3(:),stdy3(:),'marker','.'); %?plot?
xlabel('std dev of \pi');
ylabel('std dev of y');
title('Policy Frontier');
grfun.bottomlegend('Discretion','Commitment');

for i = 1 : 10 : N %?annotate?
    label = ['\lambda1 = ',sprintf('%.2f',lmb1(i))];
    
    te = text(stdpi2(i)+0.05,stdy2(i)+0.1,label, ...
        'color',get(h2,'color'),'fontSize',8);
    
    text(stdpi3(i)-0.05,stdy3(i)-0.1,label, ...
        'verticalAlignment','top','horizontalAlignment','right', ...
        'color',get(h3,'color'),'fontSize',8);
end

%% Help on IRIS functions used in this m-file
%
% Use either `help` to display help in the command window, or `idoc`
% to display help in an HTML browser window.
%
%    help model
%    help model/model
%    help model/alter
%    help model/subsasgn
%    help model/solve
%    help model/acf
%    help grfun/bottomlegend
